﻿using ADManage.api.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Http;
using XF.Meeting.Api.Models;
using XF.Meeting.Entity.CommonEntity;
using XF.Meeting.Entity.dbEntity;
using XF.Meeting.Entity.DTO;
using XF.Meeting.Entity.Query;
using XF.Meeting.Entity.View;

namespace XF.Meeting.Api.Controllers
{
    public class GroupController : BaseController
    {
        [HttpPost]
        public async Task<IHttpActionResult> GetGroupList()
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var result = await service.GetGroupList();
            var res = new ResultBase<List<GroupInfoDTO>>()
            {
                data = result,
                success = true,
                errormsg = ""
            };
            var data = new ObjBase<ResultBase<List<GroupInfoDTO>>>()
            {
                obj = res,
                secret = "123"
            };
            return Content(HttpStatusCode.OK, data);

        }
        [HttpPost]
        public async Task<IHttpActionResult> GetUserListByGroupId([FromBody] ObjBase<UserQuery> query)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            PageList<GroupUserInfoDTO> list = await service.GetUserListByGroupId(query.obj);
            return null;
        }
        [HttpPost]
        public async Task<IHttpActionResult> Insert([FromBody] ObjBase<GroupInfo> obj)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (result,des) = await service.Insert(obj.obj);
            return null;
        }
        [HttpPost]
        public async Task<IHttpActionResult> Delete([FromBody] ObjBase<GroupInfo> obj)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (result, des) = await service.Delete(obj.obj);
            return null;
        }
        public async Task<IHttpActionResult> AddUser([FromBody] ObjBase<GroupInfo> obj)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (result, des) = await service.AddUser(obj.obj);
            return null;
        }
        public async Task<IHttpActionResult> RemoveUser([FromBody] ObjBase<GroupInfo> obj)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (result, des) = await service.RemoveUser(obj.obj);
            return null;
        }

        /// <summary>
        /// 获取群组    Mobile
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IHttpActionResult> GetGroupForMobile([FromBody]ObjBase<GroupQuery> query)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var list = await service.GetGroupForMobile(query.obj);

            if (list != null)
            {
                var res = new ResultBase<PageListMobile<GroupInfoDTO>>()
                {
                    data = list,
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<PageListMobile<GroupInfoDTO>>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = "获取群组失败",
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 获取群组根节点信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> GetGroupOne([FromBody]ObjBase<GroupQuery> query)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var list = await service.GetGroupOne(query.obj);

            if (list != null)
            {
                var res = new ResultBase<PageList<GroupInfoDTO>>()
                {
                    data = list,
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<PageList<GroupInfoDTO>>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = "获取群组根节点失败",
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        ///根据父ID获取区域根节点信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> GetGroupByPID([FromBody]ObjBase<GroupQuery> query)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var list = await service.GetGroupByPID(query.obj);

            if (list != null)
            {
                var res = new ResultBase<List<GroupInfoDTO>>()
                {
                    data = list,
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<List<GroupInfoDTO>>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = "获取群组信息失败",
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 添加群组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> AddGroup([FromBody]ObjBase<GroupInfo> entity)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (bResult, strError) = await service.AddGroup(entity.obj);

            if (bResult)
            {
                var res = new ResultBase<String>()
                {
                    data = "添加群组成功",
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<String>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = strError,
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 删除群组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> DeleteGroup([FromBody]ObjBase<GroupInfo> entity)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (bResult, strError) = await service.DeleteGroup(entity.obj);

            if (bResult)
            {
                var res = new ResultBase<String>()
                {
                    data = "删除群组成功",
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<String>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = strError,
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 修改群组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> UpdateGroup([FromBody]ObjBase<GroupInfo> entity)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (bResult, strError) = await service.UpdateGroup(entity.obj);

            if (bResult)
            {
                var res = new ResultBase<String>()
                {
                    data = "修改群组成功",
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<String>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = strError,
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 群组添加用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> AddGroupUser([FromBody]ObjBase<GroupInfo> entity)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (bResult, strError) = await service.AddGroupUser(entity.obj);

            if (bResult)
            {
                var res = new ResultBase<String>()
                {
                    data = "群组添加用户成功",
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<String>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "群组添加用户成功",
                    errmsg = "",
                    success = true
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 群组更新用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> UpdateGroupUser([FromBody]ObjBase<GroupInfo> entity)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var (bResult, strError) = await service.UpdateGroupUser(entity.obj);

            if (bResult)
            {
                var res = new ResultBase<String>()
                {
                    data = "群组修改用户成功",
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<String>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = strError,
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 根据群组ID获取人员列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> GetUserListByGroupIdAndKey([FromBody]ObjBase<GroupQuery> query)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            query.obj.CreatedUserID = user.UserId;
            var list = await service.GetUserListByGroupIdAndKey(query.obj);

            if (list != null)
            {
                var res = new ResultBase<PageList<UserInfoForSearch>>()
                {
                    data = list,
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<PageList<UserInfoForSearch>>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = "获取群组人员失败",
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }

        /// <summary>
        /// 根据群组ID获取人员列表（预约编辑回显）
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IHttpActionResult> GetUserListByGroupIdForUpdate([FromBody]ObjBase<GroupQuery> query)
        {
            var dic = User.Identity as ClaimsIdentity;
            var user = GetUser(dic);
            var service = abs.CreateGroupInfoInterface(user.UserName, IPHelper.GetIPAddress);
            var list = await service.GetUserListByGroupIdForUpdate(query.obj);

            if (list != null)
            {
                var res = new ResultBase<PageList<UserInfoForSearch>>()
                {
                    data = list,
                    success = true,
                    errormsg = ""
                };
                var data = new ObjBase<ResultBase<PageList<UserInfoForSearch>>>()
                {
                    obj = res,
                    secret = "123"
                };
                return Content(HttpStatusCode.OK, data);
            }
            else
            {
                var result = new ErrorBase()
                {
                    data = "",
                    errmsg = "获取群组人员失败",
                    success = false
                };
                return Content(HttpStatusCode.OK, new ObjBase<ErrorBase>()
                {
                    obj = result,
                    secret = "123"
                }
                );
            }
        }
    }
}
